package atm;

import lovo01.ATM04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 登录界面
 */
public class LoginPanel extends JPanel implements ActionListener {
    private int errorCount = 3;
    private ATM05 atm05;
    private JLabel accountLabel = new JLabel("账号:");
    private JLabel pwdLabel = new JLabel("密码:");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(new ImageIcon("images/buttonLand.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);

    public LoginPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        //账号和密码及输入框，登录按钮
        accountLabel.setBounds(300,200,80,50);
        accountLabel.setFont(font);
        accountTextField.setBounds(380,200,250,50);
        accountTextField.setFont(font);
        pwdLabel.setBounds(300,250,80,50);
        pwdLabel.setFont(font);
        pwdField.setBounds(380,250,250,50);
        pwdField.setFont(font);
        loginBtn.setBounds(420,320,140,50);
        //建立联系
        loginBtn.addActionListener(this);

        //加进容器
        this.add(accountLabel);
        this.add(accountTextField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);

        this.setOpaque(false);
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public void setPwdField(JPasswordField pwdField) {
        this.pwdField = pwdField;
    }

    public JTextField getAccountTextField() {
        return accountTextField;
    }

    public void setAccountTextField(JTextField accountTextField) {
        this.accountTextField = accountTextField;
    }

    public void setAccountLabel(JLabel accountLabel) {
        this.accountLabel = accountLabel;
    }

    public int getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<Users> users = atm05.getUsers();
        String account = accountTextField.getText();
        String pwd = new String(pwdField.getPassword());
        for (int i = 0;i < users.size();i++){
            if (users.get(i).getName().equals(account) && users.get(i).getPwd().equals(pwd)){
                atm05.setLoginUser(users.get(i));
                atm05.showPanel("menu");
                return;
            }
        }
        errorCount--;
        if (errorCount > 0){
            JOptionPane.showMessageDialog(this,"还剩" + errorCount + "次机会");
        }else {
            JOptionPane.showMessageDialog(this,"机会已用完，卡已被吞");
            System.exit(0);
        }
    }

}
